| Conditions | 1 |
| Paths | 1 |
| Total Lines | 76 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var table_methods = { |
||
| 3 | BuildRequest: function(rq){ |
||
| 4 | Sort(rq); |
||
| 5 | Filter(rq); |
||
| 6 | |||
| 7 | function Sort(rq){ |
||
| 8 | function sortBySpan(span, i){ |
||
| 9 | var order = span.innerHTML; |
||
| 10 | if(order.length === 1){ |
||
| 11 | rq.colNo = i; |
||
| 12 | rq.colOrd = order === rq.strDesc ? "desc" : "asc"; |
||
| 13 | } |
||
| 14 | return rq.colNo === i; |
||
| 15 | } |
||
| 16 | var thTags = document.getElementById(rq.tableId) |
||
| 17 | .getElementsByTagName("thead")[0] |
||
| 18 | .getElementsByTagName("th"); |
||
| 19 | var length = thTags.length; |
||
| 20 | for(var i = 0; i < length; i++){ |
||
| 21 | var link = thTags[i].getElementsByTagName("a")[0]; |
||
| 22 | if(link){ |
||
| 23 | var span = link.getElementsByTagName("span")[0]; |
||
| 24 | if(span && sortBySpan(span, i)){ |
||
| 25 | break; |
||
| 26 | } |
||
| 27 | } |
||
| 28 | } |
||
| 29 | } |
||
| 30 | function Filter(rq){ |
||
| 31 | function getFilterFieldsByTableID(tableID){ |
||
| 32 | var fields = {filterBy: null, filter: null}; |
||
| 33 | var filterDiv = getFilterDivByTableIDOrNull(tableID); |
||
| 34 | if(filterDiv !== null){ |
||
| 35 | setFilterBy(fields, filterDiv); |
||
| 36 | setFilterValue(fields, filterDiv); |
||
| 37 | } |
||
| 38 | return fields; |
||
| 39 | } |
||
| 40 | function getFilterDivByTableIDOrNull(tableID){ |
||
| 41 | if(document.getElementById(tableID).parentNode |
||
| 42 | .getElementsByTagName("div").length > 0 |
||
| 43 | ){ |
||
| 44 | var containerDivs = document.getElementById(tableID) |
||
| 45 | .parentNode.getElementsByTagName("div"); |
||
| 46 | for(var i = 0; i < containerDivs.length; i++){ |
||
| 47 | if(containerDivs[i].getAttribute("class") === "filter"){ |
||
| 48 | return containerDivs[i]; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | } |
||
| 53 | return null; |
||
| 54 | } |
||
| 55 | function setFilterBy(fields, filterDiv){ |
||
| 56 | var select = filterDiv.getElementsByTagName("select")[0]; |
||
| 57 | if(select && |
||
| 58 | select.options[select.selectedIndex].value !== "all" |
||
| 59 | ){ |
||
| 60 | fields.filterBy = select.options[select.selectedIndex].value; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | function setFilterValue(fields, filterDiv){ |
||
| 64 | var textObj = filterDiv.getElementsByTagName("input")[0]; |
||
| 65 | if(textObj && textObj.value && textObj.value.length !== 0){ |
||
| 66 | fields.filter = encodeURIComponent(textObj.value.trim()); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | var r = getFilterFieldsByTableID(rq.tableId); |
||
| 71 | if(r.filter !== null){ |
||
| 72 | rq.filter = r.filter; |
||
| 73 | } |
||
| 74 | if(r.filterBy !== null){ |
||
| 75 | rq.filterBy = r.filterBy; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | }, |
||
| 79 | Draw: { |
||
| 286 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: